package com.litong.litongjiaoyu.service.impl;

import com.litong.litongjiaoyu.dao.CourseDao;
import com.litong.litongjiaoyu.entity.SysCourse;
import com.litong.litongjiaoyu.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.StringJoiner;

/**
 * @ClassName CourseServiceImpl
 * @Description TODO
 * @Author jingz
 * @Date 2019/8/29 14:46
 * @Version 1.0
 **/
@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseDao courseDao;

    /**
     * 获取课程列表
     *
     * @return
     */
    @Override
    public List<SysCourse> query() {
        List<SysCourse> courses = courseDao.query();

        for (SysCourse cours : courses) {
            String img = cours.getImg();

            if (img != null) {
                cours.setImageUrl(img.split(","));

                cours.setImg(null);
            }
        }

        return courses;
    }

    /**
     * 获取主页推荐课程
     *
     * @return
     */
    @Override
    public List<SysCourse> getRecommend() {
        List<SysCourse> sysCourses = courseDao.getRecommend();
        for (SysCourse sysCours : sysCourses) {
            String img = sysCours.getImg();
            sysCours.setImageUrl(img.split(","));
        }
        return sysCourses;
    }

    /**
     * 获取试听课程列表
     *
     * @return
     */
    @Override
    public List<SysCourse> getAudition() {
        List<SysCourse> sysCourses = courseDao.getAudition();
        for (SysCourse sysCours : sysCourses) {
            String img = sysCours.getImg();
            sysCours.setImageUrl(img.split(","));
        }
        return sysCourses;
    }

    /**
     * 获取除试听课程外的所有课程
     *
     * @return
     */
    @Override
    public List<SysCourse> getExcellentCourses() {
        List<SysCourse> sysCourses = courseDao.getExcellentCourses();
        for (SysCourse sysCours : sysCourses) {
            String img = sysCours.getImg();
            sysCours.setImageUrl(img.split(","));
        }
        return sysCourses;
    }

    /**
     * 获取某课程全部内容
     *
     * @param id
     * @return
     */
    @Override
    public SysCourse getCourse(long id) {

        SysCourse sysCourse = courseDao.getCourse(id);

        String img = sysCourse.getImg();

        sysCourse.setImageUrl(img.split(","));

        sysCourse.setImg(null);

        return sysCourse;
    }

    /**
     * 添加课程
     *
     * @param sysCourse
     * @return
     */
    @Transactional
    @Override
    public int insert(SysCourse sysCourse) {

        StringJoiner joiner = new StringJoiner(",");

        for (String s : sysCourse.getImageUrl()) {
            joiner.add(s);
        }

        sysCourse.setImg(joiner.toString());

        return courseDao.insert(sysCourse);
    }

    /**
     * 获取图片信息
     *
     * @param id
     * @return
     */
    @Override
    public String[] getPic(long id) {
        String img = courseDao.getPic(id);

        String[] imgs = img.split(",");

        return imgs;
    }

    /**
     * 更新图片信息
     *
     * @param id
     * @param img
     * @return
     */
    @Transactional
    @Override
    public int updatePic(long id, String[] img) {
        StringJoiner stringJoiner = new StringJoiner(",");

        for (String s : img) {
            stringJoiner.add(s);
        }

        return courseDao.updatePic(id, stringJoiner.toString());
    }

    /**
     * 更新课程信息
     *
     * @param sysCourse
     * @return
     */
    @Transactional
    @Override
    public int update(SysCourse sysCourse) {
        String[] imgs = sysCourse.getImageUrl();

        StringJoiner joiner = new StringJoiner(",");

        for (String img : imgs) {
            joiner.add(img);
        }

        sysCourse.setImg(joiner.toString());

        return courseDao.update(sysCourse);
    }

    /**
     * 单个删除
     *
     * @param id
     * @return
     */
    @Transactional
    @Override
    public int delete(long id) {
        return courseDao.delete(id);
    }

    /**
     * 批量删除
     *
     * @param id
     * @return
     */
    @Transactional
    @Override
    public int batchDelete(long[] id) {
        return courseDao.batchDelete(id);
    }
}
